package com.example.restful02.controller;

import com.example.restful02.model.Student;
import com.example.restful02.service.StudentService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "用户管理相关接口")
@RestController
public class StudentController {
    @Autowired
    StudentService studentService;

    @CrossOrigin(origins = "http://localhost:8080")
    @GetMapping("/students")
    public List<Student> getAllStudents(){
        return studentService.list();
    }


    @GetMapping("/student/{id}")
    public Student getStudentById(@PathVariable("id") Integer id){
        Student student = studentService.getById(id);
        return student;
    }

    @PostMapping("/student")
    public String addStudent(@RequestBody Student student){
        if (studentService.save(student)){
            return "插入用户成功！";
        }
        else return "插入用户失败";
    }

    @PutMapping("/student")
    public String updateStudent(@RequestBody Student student){
        if (studentService.updateById(student)){
            return "更新用户成功！";
        }
        else return "更新用户失败";

    }

    @DeleteMapping("/student")
    public String deleteStudentById(Integer id){
        if (studentService.removeById(id)){
            return "删除用户成功！";
        }
        else return "删除用户失败！";
    }
}